package by.jabbby.service;

import java.util.ArrayList;
import java.util.List;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import by.jabbby.JabbbyException;
import by.jabbby.Logger;

public class JabberServiceProxy {
	private IJabberService jabberService = null;
	private Context context = null;
	// private IBinder binder;
	private static JabberServiceProxy proxy;

	private static List<IJabberServiceBind> listeners;

	public interface IJabberServiceBind {
		public void onJabberServiceBinded();
	}
	
	static{
		listeners = new ArrayList<IJabberServiceBind>();
	}
	
	public static JabberServiceProxy getInstance(Context context) throws JabbbyException {
		Context appContext = context.getApplicationContext();
		if (proxy == null) {
			proxy = new JabberServiceProxy(appContext);
			Logger.i("JabberSERVICEProxy create new instance");
		} else {
			/*
			if (proxy.context != appContext) {
				proxy.unbindService();
				proxy.context = appContext;
				proxy.bindService();
				Logger.i("JabberSERVICEProxy change context");
			}
			*/
		}
		return proxy;
	}
	
	public static void addBinderListener(IJabberServiceBind listener) {
		listeners.add(listener);
		/*if (jabberService != null) {
			listener.onJabberServiceBinded();
		}
		*/
	}

	public static void removeBinderListener(IJabberServiceBind listener) {
		try{
		if(!listeners.remove(listener)){
			Logger.i("JabberServiceProxy.removeBinderListener(): cannot remove listener. Listener not exist");
		}
		}catch(Exception e){
			Logger.e(e);
		}
	}

	private ServiceConnection serviceConnection = new ServiceConnection() {

		public void onServiceDisconnected(ComponentName name) {
			jabberService = null;
		}

		public void onServiceConnected(ComponentName name, IBinder service) {
			jabberService = IJabberService.Stub.asInterface(service);
			for (IJabberServiceBind l : listeners) {
				l.onJabberServiceBinded();
			}
		}
	};

	private JabberServiceProxy(Context context) throws JabbbyException {
		this.context = context;
		listeners = new ArrayList<IJabberServiceBind>();
		bindService();
	}

	private void bindService() throws JabbbyException {
		Intent serviceIntent = new Intent(context, JabberService.class);
		this.context.startService(serviceIntent);

		// /getService()
		if (!this.context.bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE)) {
			throw new JabbbyException();
		}

	}

	private void unbindService() {
		// Intent serviceIntent = new Intent(context, JabberService.class);
		this.context.unbindService(serviceConnection);
	}

	public IJabberService getService() throws JabbbyException {

		if (jabberService == null) {
			bindService();
		}

		if (jabberService == null) {

			throw new JabbbyException("Error when call jabber service. Service is not bind.");

		}
		return jabberService;
	}

	
	public boolean isServiceBinded(){
		return (this.jabberService != null);
	}
}
